package com.rich.dmr.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.rich.dmr.R;

public class CommonDialog extends Dialog {

    private ImageView imageIv ;

    private TextView titleTv ;

    private TextView commonNickName,commonNickId,commonNickExtId;
    private EditText commonName;
    private EditText commonId;



    private EditText commonExtId;

    private Button negativeBn ,positiveBn;

    /**
     * 按钮之间的分割线
     */
    private View columnLineView ;
    public CommonDialog(Context context) {
        super(context, R.style.CustomDialog);
    }

    private String message;
    private String title;
    private String positive,negative ;
    private int imageResId = -1 ;
    private int inputId = -1;


    private String nickName,nickId,nickExtId;
    /**
     * 底部是否只有一个按钮
     */
    private boolean isSingle = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.commom_dialog_layout);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        refreshView();
        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        positiveBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( onClickBottomListener!= null) {
                    onClickBottomListener.onPositiveClick();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        negativeBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( onClickBottomListener!= null) {
                    onClickBottomListener.onNegativeClick();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void refreshView() {
        //如果用户自定了title和message
        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
            titleTv.setVisibility(View.VISIBLE);
        }else {
            titleTv.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(message)) {
           // commonNickName.setText(message);
        }

        if (!TextUtils.isEmpty(nickName)) {
            commonNickName.setText(nickName);
        }

        if (!TextUtils.isEmpty(nickId)) {
            commonNickId.setText(nickId);
        }else {
            commonNickId.setVisibility(View.GONE);
            commonId.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(nickExtId)) {
            commonNickExtId.setText(nickExtId);
        }else {
            commonNickExtId.setVisibility(View.GONE);
            commonExtId.setVisibility(View.GONE);
        }
        //如果设置按钮的文字
        if (!TextUtils.isEmpty(positive)) {
            positiveBn.setText(positive);
        }else {
            positiveBn.setText("Save");
        }
        if (!TextUtils.isEmpty(negative)) {
            negativeBn.setText(negative);
        }else {
            negativeBn.setText("Cancel");
        }

        if (imageResId!=-1){
            imageIv.setImageResource(imageResId);
            imageIv.setVisibility(View.VISIBLE);
        }else {
            imageIv.setVisibility(View.GONE);
        }

        if(inputId != -1){
            commonId.setInputType(inputId);
            commonExtId.setInputType(inputId);
        }
        /**
         * 只显示一个按钮的时候隐藏取消按钮，回掉只执行确定的事件
         */
        if (isSingle){
            columnLineView.setVisibility(View.GONE);
            negativeBn.setVisibility(View.GONE);
        }else {
            negativeBn.setVisibility(View.VISIBLE);
            columnLineView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void show() {
        super.show();
        refreshView();
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        negativeBn = (Button) findViewById(R.id.negtive);
        positiveBn = (Button) findViewById(R.id.positive);
        titleTv = (TextView) findViewById(R.id.title);
        commonNickName = (TextView) findViewById(R.id.common_nick_name);
        commonNickId = (TextView) findViewById(R.id.common_nick_id);
        commonNickExtId = (TextView) findViewById(R.id.common_ext_nick_id);
        commonName = (EditText) findViewById(R.id.common_name);
        commonId = (EditText) findViewById(R.id.common_id);
        commonExtId = (EditText) findViewById(R.id.common_ext_id);
        imageIv = (ImageView) findViewById(R.id.image);
        columnLineView = findViewById(R.id.column_line);
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;
    public CommonDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }
    public interface OnClickBottomListener{
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick();
        /**
         * 点击取消按钮事件
         */
        public void onNegativeClick();
    }
    public EditText getCommonName() {
        return commonName;
    }

    public EditText getCommonId() {
        return commonId;
    }

    public EditText getCommonExtId() {
        return commonExtId;
    }
    public String getNickName() {
        return nickName;
    }

    public CommonDialog setNickName(String nickName) {
        this.nickName = nickName;
        return this ;
    }

    public String getNickId() {
        return nickId;
    }

    public CommonDialog setNickId(String nickId) {
        this.nickId = nickId;
        return this ;
    }

    public CommonDialog setInputType(int id) {
        inputId = id;
        return this ;
    }

    public String getNickExtId() {
        return nickExtId;
    }

    public CommonDialog setNickExtId(String nickExtId) {
        this.nickExtId = nickExtId;
        return this ;
    }

    public String getMessage() {
        return message;
    }

    public CommonDialog setMessage(String message) {
        this.message = message;
        return this ;
    }

    public String getTitle() {
        return title;
    }

    public CommonDialog setTitle(String title) {
        this.title = title;
        return this ;
    }

    public String getPositive() {
        return positive;
    }

    public CommonDialog setPositive(String positive) {
        this.positive = positive;
        return this ;
    }

    public String getNegative() {
        return negative;
    }

    public CommonDialog setNegative(String negative) {
        this.negative = negative;
        return this ;
    }

    public int getImageResId() {
        return imageResId;
    }

    public boolean isSingle() {
        return isSingle;
    }

    public CommonDialog setSingle(boolean single) {
        isSingle = single;
        return this ;
    }

    public CommonDialog setImageResId(int imageResId) {
        this.imageResId = imageResId;
        return this ;
    }

}
